using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFrameworkLibraly.BaseObjects
{
    public class SpriteObject:GameObjectBase
    {

        public event EventHandler SpriteTextureChanged;

        private void InvokeSpriteTextureChanged(EventArgs e)
        {
            EventHandler handler = SpriteTextureChanged;
            if (handler != null) handler(this, e);
        }


        private Texture2D _spriteTexture;
        public virtual Texture2D SpriteTexture
        {
            get { return _spriteTexture; }
            set
            {
                _spriteTexture = value;
                InvokeSpriteTextureChanged(EventArgs.Empty);
            }
        }

        public float PositionX
        {
            get { return Position.X; }
            set { _position.X = value; }
        }

        public float PositionY
        {
            get { return Position.Y; }
            set { _position.Y = value; }
        }

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public float OriginX;
        public float OriginY;
        public Vector2 Origin { get { return new Vector2(OriginX, OriginY); } }
        
        public float Angle;

        public float ScaleX=1;
        public float ScaleY=1;
        public Vector2 Scale { get { return new Vector2(ScaleX, ScaleY); } }

        public Rectangle SourceRect;

        public Color SpriteColor = Color.White;

        public float LayerDepth;

        public SpriteObject(MyGame game) : base(game)
        {
        }

        public SpriteObject(MyGame game, Vector2 position):this(game)
        {
            PositionX = position.X;
            PositionY = position.Y;
        }

        public SpriteObject(MyGame game, Vector2 position, Texture2D texture):this(game,position)
        {
            SpriteTexture = texture;
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if(SpriteTexture!=null)
            {
                if (SourceRect.IsEmpty)
                    spriteBatch.Draw(SpriteTexture, Position, null, SpriteColor, Angle, Origin, Scale,SpriteEffects.None, LayerDepth);
                else
                    spriteBatch.Draw(SpriteTexture, Position, SourceRect, SpriteColor, Angle, Origin, Scale,SpriteEffects.None, LayerDepth);
            }
        }

        public virtual Rectangle BoundingBox
        {
            get
            {
                Vector2 spriteSize= SourceRect.IsEmpty 
                        ? new Vector2(SpriteTexture.Width,SpriteTexture.Height) 
                        : new Vector2(SourceRect.Width, SourceRect.Height);

                Rectangle result = new Rectangle((int)PositionX, (int)PositionY, (int)(spriteSize.X * ScaleX), (int)(spriteSize.Y * ScaleY));

                result.Offset((int) (-OriginX * ScaleX), (int) (-OriginY * ScaleY));

                return result;
            }
        }
    }
}
